package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time"
)

// 生成随机jin64值 并发送到通道中，在从通道中取值
type job struct {
	value int64
}
type result struct {
	job *job
	sum int64
}

var jobChan = make(chan *job, 100)
var resultChan = make(chan *result, 100)
var wg sync.WaitGroup

// 生产者
func zhoulin(zl chan<- *job) {
	defer wg.Done()
	// 循环生成随机数 发送到通道
	for {
		x := rand.Int63()
		newJob := &job{value: x}
		zl <- newJob
		time.Sleep(1 * time.Second)

	}
}

// 接收者
func baodelu(zl <-chan *job, resultChan chan<- *result) {
	defer wg.Done()
	for {
		job := <-zl
		n := job.value
		sum := int64(0)
		for n > 0 {
			sum += n % 10
			n /= 10
		}
		newResult := &result{job, sum}
		resultChan <- newResult
	}
}
func main() {
	wg.Add(1)
	go zhoulin(jobChan)

	for i := 0; i < 24; i++ {
		go baodelu(jobChan, resultChan)
	}

	for v := range resultChan {
		fmt.Printf("v: %v\n", v.job.value)
	}
	wg.Wait()
}
